package tech.yunqian.slim.project.domain;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import tech.yunqian.slim.project.enums.TriggerSource;

@Data
public class MonitorConditionTrackingItem {
    private MonitorCondition monitorCondition;
    private AlarmDetail alarmDetail;
    private List<TelemetryMonitorConditionOccurence> telemetryMonitorConditionOccurences;
    private List<EventMonitorConditionOccurence> eventMonitorConditionOccurences;
    private String deviceName;

    public MonitorConditionTrackingItem(String deviceName, MonitorCondition monitorCondition) {
        this.deviceName = deviceName;
        this.monitorCondition = monitorCondition;
        this.alarmDetail = null;
        this.telemetryMonitorConditionOccurences = new ArrayList<>();
        this.eventMonitorConditionOccurences = new ArrayList<>();
    }

    public Boolean appendTelemetryMonitorConditionOccurence(TelemetryMonitorConditionOccurence telemetryMonitorConditionOccurence) {
        Boolean done = true;
        if (this.telemetryMonitorConditionOccurences == null) {
            this.telemetryMonitorConditionOccurences = new ArrayList<>();
        }
        this.telemetryMonitorConditionOccurences.add(telemetryMonitorConditionOccurence);
        return done;
    }

    public Boolean appendEventMonitorConditionOccurence(EventMonitorConditionOccurence eventMonitorConditionOccurence) {
        Boolean done = true;
        if (this.eventMonitorConditionOccurences == null) {
            this.eventMonitorConditionOccurences = new ArrayList<>();
        }
        this.eventMonitorConditionOccurences.add(eventMonitorConditionOccurence);
        return done;
    }

    public Boolean isFulfilled() {
        Integer repeatCount = monitorCondition.getRepeatCount();
        if (repeatCount != null && repeatCount > 0) {
            return this.telemetryMonitorConditionOccurences.size() >= repeatCount;
        }
        Long duration = monitorCondition.getDuration().longValue() * 1000;
        if (duration != null && duration > 0) {
            if (monitorCondition.getTriggerSource().equals(TriggerSource.TELEMETRY.getCode())) {
                Integer size = eventMonitorConditionOccurences.size();
                if (size > 0) {
                    TelemetryMonitorConditionOccurence first = telemetryMonitorConditionOccurences.get(0);
                    TelemetryMonitorConditionOccurence last = telemetryMonitorConditionOccurences.get(size - 1);
                    return last.getTimestamp().getTime() - first.getTimestamp().getTime() > duration;
                }
            } else if (monitorCondition.getTriggerSource().equals(TriggerSource.EVENT.getCode())) {
                Integer size = eventMonitorConditionOccurences.size();
                if (size > 0) {
                    EventMonitorConditionOccurence first = eventMonitorConditionOccurences.get(0);
                    EventMonitorConditionOccurence last = eventMonitorConditionOccurences.get(size -1);
                    return last.getTimestamp().getTime() - first.getTimestamp().getTime() > duration;
                }
            }
        }
        return false;
    }

    public void clear() {
        this.telemetryMonitorConditionOccurences.clear();
    }
}
